package org.ycx.doc.service.impl;

import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.doc.dao.auto.DocGroupUserMapper;
import org.ycx.doc.dao.enums.GroupUsersRole;
import org.ycx.doc.dao.mapper.IDocGroupMapper;
import org.ycx.doc.dao.mapper.IDocProjectMapper;
import org.ycx.doc.dao.model.DocGroup;
import org.ycx.doc.dao.model.DocGroupExample;
import org.ycx.doc.dao.model.DocGroupUser;
import org.ycx.doc.dao.model.DocProject;
import org.ycx.doc.dto.DocUser;
import org.ycx.doc.service.IDocGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ycx.upms.dao.model.SecUser;

import java.util.List;

/**
 * DocGroupService实现
 * Created by yang on 2017-08-30 17:02:41.
 */
@Service
@Transactional
@BaseService
public class DocGroupServiceImpl extends BaseServiceImpl<IDocGroupMapper, DocGroup, DocGroupExample> implements IDocGroupService {

    private static Logger _log = LoggerFactory.getLogger (DocGroupServiceImpl.class);

    @Autowired
    IDocGroupMapper mapper;

    @Autowired
    IDocProjectMapper projectMapper;

    @Override
    public List<DocGroup> findGroupByUser(Integer uid, GroupUsersRole type) {
        return this.mapper.findDocGroupByUser (uid, type.getType ());
    }

    @Override
    public List<DocProject> findGroupProjects(Integer gid) {
        return this.projectMapper.findProjectByGid (gid);
    }

    @Override
    public int createGroup(DocGroup bean) {
        DynamicDataSource.setDataSource (DataSourceEnum.MASTER.getName ());
        int groupKey = this.mapper.insertSelective (bean);
        DocGroupUser docGroupUser = new DocGroupUser ();
        docGroupUser.setGroupId (bean.getGroupId ());
        docGroupUser.setUserId (bean.getCreateUid ());
        docGroupUser.setRole (GroupUsersRole.Master.getType ());
        return mapper.insertGroupUser (docGroupUser);
    }

    @Override
    public int delGroup(Integer id) {
        int res = this.mapper.deleteByPrimaryKey (id);
        return res;
    }

    @Override
    public int findGroupUserRole(int gid, int uid) {
        return this.mapper.findGroupUserRole (gid, uid);
    }

    @Override
    public List<DocUser> findGroupUser(Integer gid) {
        return this.mapper.findGroupUser (gid);
    }

    @Override
    public DocUser addGroupUser(String user, Integer gid, Integer role) {
        List<DocUser> users = this.mapper.findUserByLoginName (user);
        DocUser bean = users.get (0);
        int res = this.mapper.addGroupUser (bean.getUid (), gid, role);
        if (res > 0) {
            bean.setRole (role);
            return bean;
        } else {
            return null;
        }
    }

    @Override
    public int findUser(String loginName) {
        List<DocUser> users = this.mapper.findUserByLoginName (loginName);
        return users.size ();
    }

    @Override
    public int removeGroupUser(Integer uid, Integer gid) {
        DynamicDataSource.setDataSource (DataSourceEnum.MASTER.getName ());
        int role = this.mapper.findGroupUserRole (gid, uid);
        int masterCount = 0;
        if (role == GroupUsersRole.Master.getType ()) {
            masterCount = this.mapper.countGroupUserByRole (gid, GroupUsersRole.Master.getType ());
        } else {
            masterCount = 2;
        }
        int res = 0;
        if (masterCount > 1) {
            res = this.mapper.deleteGroupUser (gid, uid);
        } else {
            res = 0;
        }
        DynamicDataSource.clearDataSource ();
        return res;
    }

    @Override
    public DocUser findGroupUserById(Integer gid, Integer uid) {
        DynamicDataSource.setDataSource (DataSourceEnum.SLAVE.getName ());
        DocUser user = this.mapper.findGroupUserById(gid,uid);
        DynamicDataSource.clearDataSource ();
        return user;
    }
}